SECTION= 'server_engine'
class BaseServer:
    def main(self, config, **kargs):
        self.options = kargs
        self.config = config
        self.host = config.get(SECTION, 'host', '127.0.0.1')
        self.port = int(config.get(SECTION, 'port', 3000))
        
class GeventServer(BaseServer):
    def run(self, app):
        from gevent.wsgi import WSGIServer
        #print "gevent server on (%s %d)" % (self.host, self.port)
        address = self.host, self.port
        server = WSGIServer(address, app)
        #server.backlog = 256
        server.serve_forever()

class TwistedServer(BaseServer):
    def run(self, app):
        from twisted.web.server import Site
        from twisted.web.wsgi import WSGIResource
        from twisted.internet import reactor
        resource = WSGIResource(reactor, reactor.getThreadPool(), app)
        reactor.listenTCP(self.port, Site(resource))
        reactor.run()
        
class CGIServer(BaseServer):
    quiet = True
    def run(self, handler): # pragma: no cover
        from wsgiref.handlers import CGIHandler
        CGIHandler().run(handler) # Just ignore host and port here
        
class FlupFCGIServer(BaseServer):
    def run(self, handler): # pragma: no cover
        import flup.server.fcgi
        flup.server.fcgi.WSGIServer(handler, bindAddress=(self.host, self.port)).run()
        
class WSGIRefServer(BaseServer):
    def run(self, handler): # pragma: no cover
        from wsgiref.simple_server import make_server, WSGIRequestHandler
        if self.quiet:
            class QuietHandler(WSGIRequestHandler):
                def log_request(*args, **kw): pass
            self.options['handler_class'] = QuietHandler
        srv = make_server(self.host, self.port, handler, **self.options)
        srv.serve_forever()

class CherryPyServer(BaseServer):
    def run(self, handler): # pragma: no cover
        from cherrypy import wsgiserver
        server = wsgiserver.CherryPyWSGIServer((self.host, self.port), handler)
        server.start()
        
class PasteServer(BaseServer):
    def run(self, handler): # pragma: no cover
        from paste import httpserver
        from paste.translogger import TransLogger
        app = TransLogger(handler)
        httpserver.serve(app, host=self.host, port=str(self.port), **self.options)
        
class FapwsServer(BaseServer):
    """
    Extremly fast webserver using libev.
    See http://william-os4y.livejournal.com/
    """
    def run(self, handler): # pragma: no cover
        import fapws._evwsgi as evwsgi
        from fapws import base

        evwsgi.start(self.host, str(self.port))
        evwsgi.set_base_module(base)

        evwsgi.wsgi_cb(('',handler))
        evwsgi.set_debug(0)
        evwsgi.run()
        
class TornadoServer(BaseServer):
    """ Untested. As described here:
        http://github.com/facebook/tornado/blob/master/tornado/wsgi.py#L187 """
    def run(self, app): # pragma: no cover
        import tornado.wsgi
        import tornado.httpserver
        import tornado.ioloop
        container = tornado.wsgi.WSGIContainer(app)
        server = tornado.httpserver.HTTPServer(container)
        server.listen(port=self.port)
        tornado.ioloop.IOLoop.instance().start()

class AppEngineServer(BaseServer):
    """ Untested. """
    quiet = True
    def run(self, app):
        from google.appengine.ext.webapp import util
        util.run_wsgi_app(app)
        
class TwistedServer(BaseServer):
    """ Untested. """
    def run(self, app):
        from twisted.web import server, wsgi
        from twisted.python.threadpool import ThreadPool
        from twisted.internet import reactor
        thread_pool = ThreadPool()
        thread_pool.start()
        reactor.addSystemEventTrigger('after', 'shutdown', thread_pool.stop)
        factory = server.Site(wsgi.WSGIResource(reactor, thread_pool, app))
        reactor.listenTCP(self.port, factory, interface=self.host)
        reactor.run()
        
class DieselServer(BaseServer):
    """ Untested. """
    def run(self, app):
        from diesel.protocols.wsgi import WSGIApplication
        app = WSGIApplication(app, port=self.port)
        app.run()

class GunicornServer(BaseServer):
    """ Untested. """
    def run(self, app):
        import gunicorn.arbiter
        gunicorn.arbiter.Arbiter((self.host, self.port), 4, app).run() 

class EventletServer(BaseServer):
    """ Untested """
    def run(self, app):
        from eventlet import wsgi, listen
        wsgi.server(listen((self.host, self.port)), app)



class RocketServer(BaseServer):
    """ Untested. As requested in issue 63
        http://github.com/defnull/bottle/issues/#issue/63 """
    def run(self, app):
        from rocket import Rocket
        server = Rocket((self.host, self.port), 'wsgi', {'wsgi_app':app})
        server.start()